var shell = require('shelljs')
const env = require('./env')
const fs = require('fs')
const { resolve } = require('path')
const versionBuffer = fs.readFileSync(resolve(__dirname, './publish/.version'))
const version = versionBuffer.toString().replace(new RegExp('\n'), '')
const a = version.split('.')[0]
const b = version.split('.')[1]
const c = version.split('.')[2]
const { Version, versionControl } = require('./publish/version')
const version1 = new Version(a, b, c)
versionControl.addVersion(version1)
versionControl.getVersion(0).increment('patch')
const newVersion = versionControl.getVersion(0).toString()
fs.writeFileSync(resolve(__dirname, './publish/.version'), versionControl.getVersion(0).toString())
const execFn = (serviceOutput) => {
  shell.exec(`
  git pull
  git commit -m '代码发布-'${+new Date()}
  git push
  tar cvzf ${env.dir}.tar.gz ${env.dir}/
  ssh ${env.ssh} '
    rm -rf ${serviceOutput}${env.dir}
    mkdir -p ${serviceOutput}${env.dir}
  '
  scp -r ${env.dir}.tar.gz ${env.ssh}:${serviceOutput}
  ssh ${env.ssh} '
    cd ${serviceOutput}
    tar cvzf ${env.dir}-${newVersion}.tar.gz ${env.dir}/
    rm -rf ${serviceOutput}${env.dir}
    tar -zxvf ${serviceOutput}${env.dir}.tar.gz
  '
  rm -rf ${env.dir}.tar.gz
  clear
  `)
}

const serviceOutputList = env.serviceOutputList;
serviceOutputList.forEach((item)=>{
  execFn(item)
})
